
package ma.crawler.mplayer.sogou.provider;


import ma.http.HttpUtils;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class SogouClient {

    private static final String URL = "http://mp3.sogou.com";

    private static final String PATH_MUSIC = "/music.so";

    private static final String DEFAULT_ENCODING = "GBK";

    public String requestHtml(String keyword) {
        String url = getUrl(keyword);
        return HttpUtils.getUrlAsString(url);
    }
    
    public String getUrl(String keyword) {
        String query = getQuery(keyword);
        String url = URL + PATH_MUSIC + "?" + query;
        return url;
    }

    private String getQuery(String query) {
        List<NameValuePair> p = new ArrayList<NameValuePair>();
        p.add(new BasicNameValuePair("query", query));
        p.add(new BasicNameValuePair("class", "1"));
        p.add(new BasicNameValuePair("pf", "mp3"));
        p.add(new BasicNameValuePair("w", "02009900"));
        p.add(new BasicNameValuePair("st", ""));
        p.add(new BasicNameValuePair("ac", "1"));
        p.add(new BasicNameValuePair("sourcetype", "sugg"));
        p.add(new BasicNameValuePair("p", "40030200"));
        p.add(new BasicNameValuePair("dp", "1"));
        p.add(new BasicNameValuePair("_asf", "mp3.sogou.com"));
        p.add(new BasicNameValuePair("_ast", "1370481502"));
        return URLEncodedUtils.format(p, DEFAULT_ENCODING);
    }

    public static String getDownloadPage(String query) {
        return URL + query;
    }

}
